﻿/****************************** Module Header ******************************\
 * Module Name:  _Url.cs
 * Module Version:  1.0.0 (2012-9-16)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used for _Request.
 * 由UriInfomation改进而来，用于替代UriInfomation
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using UCL;

namespace UCL.Web
{
    public class _Url
    {
        /// <summary>
        /// 获取URL信息
        /// <para>如：http://t.cn/read.aspx?id=1 返回 http://t.cn/read.aspx?id=1 </para>
        /// </summary>
        public static Uri Url { get { return HttpContext.Current.Request.Url; } }
        private static _Url instance;
        private static string originalString;
        public static _Url Current
        {
            get
            {
                if (instance == null || Url.OriginalString != originalString)
                {
                    originalString = Url.OriginalString;
                    instance = new _Url();
                }
                return instance;
            }
        }
        protected _Url()
        {
            HttpRequest hr = System.Web.HttpContext.Current.Request;
            this.Paras = hr.QueryString.ToDictionary();
            this.BaseFileName = hr.Url.LocalPath;
        }
        private Dictionary<string, object> Paras;

        /// <summary>
        /// 返回当前执行脚本的页面名称
        /// <para>如：http://t.cn/read.aspx?id=1 返回 read.aspx </para>
        /// </summary>
        public string BaseFileName { get; set; }

        public _Url AddP(string name, object value)
        {
            instance.Paras.Add(name, value);
            return instance;
        }
        public _Url RemoveP(string name)
        {
            instance.Paras.Remove(name);
            return instance;
        }
        public _Url UpdateP(string name, object value)
        {
            instance.Paras[name] = value;
            return instance;
        }
        public string ToQueryString()
        {
            if (this.Paras.Count == 0) return string.Empty;
            StringBuilder sb = new StringBuilder();
            sb.Append("?");
            foreach (string s in this.Paras.Keys)
            {
                sb.AppendFormat("{0}={1}", s, this.Paras[s]);
            }
            return sb.ToString();
        }
        public string ToAbsoluteUrl()
        {
            return "";
        }

        /// <summary>
        /// 获取指定节点的路径段的值
        /// <para>如：http://t.cn/a/b.aspx?id=1 返回 /,a/,b.aspx</para>
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public string GetSegment(int index)
        {
            string[] a = Url.Segments;
            return (a.Length < index) ? string.Empty:a[index];
        }

        /// <summary>
        /// URI转换为路径
        /// <para>如 /a/b/c=>可点击的逐级路径</para>
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="format"></param>
        /// <param name="split"></param>
        /// <returns></returns>
        public static string ToLocation(string uri, string format = "<a href='?path={1}'>{0}</a>", string split = " / ")
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            string spath = uri;
            int len = 0;
            foreach (string s in uri.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Reverse())
            {
                spath = spath.Substring(0, spath.Length - len);
                len = s.Length + 1;
                sb.Insert(0, format.Fmt(s, spath) + split);
            }
            if (uri.StartsWith("/")) sb.Insert(0, format.Fmt("/", "/") + split);
            return sb.Remove(sb.Length - split.Length, split.Length).ToString().Replace("//", "/");
        }

        public static void TestUri()
        {
            _Response.Write("<table border='1' cellspacing='0' cellpadding='0'>");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.ApplicationPath", _Request.Request.ApplicationPath,
                "返回当前应用程序的根目录路径",
                "如：http://t.cn/read.aspx?id=1 返回 / 或 http://t.cn/blog/read.aspx?id=1 返回 /blog");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.AppRelativeCurrentExecutionFilePath", _Request.Request.AppRelativeCurrentExecutionFilePath,
            "获取应用程序根的虚拟路径，并通过对应用程序根使用波形符 (~) 表示法（例如，以“~/page.aspx”的形式）使该路径成为相对路径",
            "如：http://t.cn/a/read.aspx?id=1 返回 ~/a/read.aspx");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Path", _Request.Request.Path,
                "获取当前请求的虚拟路径",
                "如：http://t.cn/a/read.aspx?id=1 返回 /a/read.aspx");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.CurrentExecutionFilePath", _Request.Request.CurrentExecutionFilePath,
                "获取当前请求的虚拟路径",
                "如：http://t.cn/a/read.aspx?id=1 返回 /a/read.aspx");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.FilePath", _Request.Request.FilePath,
                "返回当前请求的虚拟路径,相当于WebApplication的Path",
                "如：http://t.cn/blog/read.aspx?id=1 返回 /blog/read.aspx");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.RawUrl", _Request.Request.RawUrl,
                "获取当前请求的原始 URL",
                "如：http://t.cn/blog/read.aspx?id=1 返回 /blog/read.aspx?id=1");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", 
                "Request.Url", _Request.Request.Url,
                "获取有关当前请求的 URL 的信息",
                "http://t.cn/blog/read.aspx?id=1 返回 http://t.cn/blog/read.aspx?id=1");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.QueryString", _Request.Request.QueryString.ToString(),
                "获取当前请求的查询字符串变量集合",
                "如：http://t.cn/blog/read.aspx?id=1 返回 id=1");


            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.CurrentExecutionFilePathExtension", _Request.Request.CurrentExecutionFilePathExtension,
                "获取 System.Web.HttpRequest.CurrentExecutionFilePath 属性中指定的文件名的扩展名",
                "如：http://t.cn/blog/read.aspx?id=1 返回 .aspx");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.UrlReferrer", _Request.Request.UrlReferrer,
                "获取客户端上次请求的信息",
                "如：http://t.cn/read.aspx?id=1 返回 http://t.cn/default.aspx");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.PhysicalApplicationPath", _Request.Request.PhysicalApplicationPath,
                "获取当前正在执行的服务器应用程序的根目录的物理文件系统路径",
                @"如 http://t.cn/read.aspx?id=1 返回 F:\PDC\ProjectEnv\xyx1012\UAS.DMS.Web\");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.PhysicalPath", _Request.Request.PhysicalPath,
                "获取与请求的 URL 相对应的物理文件系统路径",
                @"如 http://t.cn/a/read.aspx?id=1 返回 F:\PDC\ProjectEnv\xyx1012\UAS.DMS.Web\a\read.aspx ");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.RequestType", _Request.Request.RequestType,
                "获取或设置客户端使用的 HTTP 数据传输方法（GET 或 POST）",
                "如 http://t.cn/a/read.aspx?id=1 返回 GET");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.HttpMethod", _Request.Request.HttpMethod,
                "获取客户端使用的 HTTP 数据传输方法（如 GET、POST 或 HEAD）",
                "如 http://t.cn/a/read.aspx?id=1 返回 GET");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.UserHostAddress", _Request.Request.UserHostAddress,
                "获取远程客户端的 IP 主机地址",
                "如 http://t.cn/a/read.aspx?id=1 返回 127.0.0.1");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.UserHostName", _Request.Request.UserHostName,
                "获取远程客户端的 DNS 名称",
                "如 http://t.cn/a/read.aspx?id=1 返回 127.0.0.1");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.UserLanguages", _Request.Request.UserLanguages.ToString(","),
                "获取客户端语言首选项的排序字符串数组",
                "如 http://t.cn/a/read.aspx?id=1 返回 zh-CN");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.ContentLength", _Request.Request.ContentLength,
                "指定客户端发送的内容长度（以字节计）",
                "如 http://t.cn/a/read.aspx?id=1 返回 0");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.ContentType", _Request.Request.ContentType,
                "获取或设置传入请求的 MIME 内容类型",
                "如 http://t.cn/a/read.aspx?id=1 返回 ");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.UserAgent", _Request.Request.UserAgent,
                "获取客户端浏览器的原始用户代理信息",
                "如 http://t.cn/a/read.aspx?id=1 返回 Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.OriginalString", _Request.Request.Url.OriginalString,
                "获取传递给 System.Uri 构造函数的原始 URI 字符串",
                "如 http://t.cn/a/read.aspx?id=1 返回 http://t.cn:80/a/read.aspx?id=1");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.AbsoluteUri", _Request.Request.Url.AbsoluteUri,
                "获取绝对 URI",
                "如 http://t.cn/a/read.aspx?id=1 返回 http://t.cn/a/read.aspx?id=1");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.PathAndQuery", _Request.Request.Url.PathAndQuery,
                "获取当前请求的虚拟路径(包含完整的参数信息)",
                "如：http://t.cn/blog/read.aspx?id=1 返回 /blog/read.aspx?id=1");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.LocalPath", _Request.Request.Url.LocalPath,
                "获取文件名的本地操作系统表示形式",
                "如 http://t.cn/a/read.aspx?id=1 返回 /blog/read.aspx");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.AbsolutePath", _Request.Request.Url.AbsolutePath,
                "获取URL信息",
                "如：http://t.cn/read.aspx?id=1 返回 /blog/read.aspx");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.Segments", _Request.Request.Url.Segments.ToString(","),
                "获取包含构成指定 URI 的路径段的数组",
                "如 http://t.cn/a/read.aspx?id=1 返回 /,a/,read.aspx");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.Query", _Request.Request.Url.Query,
                "获取指定 URI 中包括的任何查询信息",
                "如 http://t.cn/a/read.aspx?id=1 返回 ?id=1");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.Scheme", _Request.Request.Url.Scheme,
                "获取此 URI 的方案名称",
                "如 http://t.cn/a/read.aspx?id=1 返回 http");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.Port", _Request.Request.Url.Port,
                "获取此 URI 的端口号",
                "如 http://t.cn/a/read.aspx?id=1 返回 80");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.Authority", _Request.Request.Url.Authority,
                "获取主机域名信息",
                "如：http://t.cn/read.aspx?id=1 返回 t.cn");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.DnsSafeHost", _Request.Request.Url.DnsSafeHost,
                "获得可安全用于 DNS 解析的未转义主机名",
                "如 http://t.cn/a/read.aspx?id=1 返回 t.cn");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.Host", _Request.Request.Url.Host,
                "获取此实例的主机部分",
                "如 http://t.cn/a/read.aspx?id=1 返回 t.cn");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.Fragment", _Request.Request.Url.Fragment,
                "获取转义 URI 片段",
                "如 http://t.cn/a/read.aspx?id=1 返回 ");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.HostNameType", _Request.Request.Url.HostNameType,
                "获取 URI 中指定的主机名的类型",
                "如 http://t.cn/a/read.aspx?id=1 返回 Dns");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.IsDefaultPort", _Request.Request.Url.IsDefaultPort,
                "获取一个值，该值指示 URI 的端口值是否为此方案的默认值",
                "如 http://t.cn/a/read.aspx?id=1 返回 True");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.IsAbsoluteUri", _Request.Request.Url.IsAbsoluteUri,
                "获取一个值，该值指示 System.Uri 实例是否为绝对 URI",
                "如 http://t.cn/a/read.aspx?id=1 返回 True");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.IsFile", _Request.Request.Url.IsFile,
                "获取一个值，该值指示指定的 System.Uri 是否为文件 URI",
                "如 http://t.cn/a/read.aspx?id=1 返回 False");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.IsLoopback", _Request.Request.Url.IsLoopback,
                "获取一个值，该值指示指定的 System.Uri 是否引用了本地主机",
                "如 http://t.cn/a/read.aspx?id=1 返回 False");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.IsUnc", _Request.Request.Url.IsUnc,
                "获取一个值，该值指示指定的 System.Uri 是否为统一命名约定 (UNC) 路径",
                "如 http://t.cn/a/read.aspx?id=1 返回 False");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.IsWellFormedOriginalString", _Request.Request.Url.IsWellFormedOriginalString(),
                "指示用于构造此 System.Uri 的字符串是否格式良好，以及它是否不需要进一步转义",
                "如 http://t.cn/a/read.aspx?id=1 返回 True");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.UserEscaped", _Request.Request.Url.UserEscaped,
                "指示 URI 字符串在创建 System.Uri 实例之前已被完全转义",
                "如 http://t.cn/a/read.aspx?id=1 返回 False");

            _Response.Write("<tr><td><div style='font-size:12px; color:#333; line-height:18px; padding:5px;'><span style='color:#F00'>{0}</span><br />{2}<br /><span style='color:#090'>{3}</span></td><td>{1}&nbsp;</td></tr>", "Request.Url.UserInfo", _Request.Request.Url.UserInfo,
                "获取用户名、密码或其他与指定 URI 关联的特定于用户的信息",
                "如 http://t.cn/a/read.aspx?id=1 返回 ");

            _Response.Write("</table>");
        }
    }
}
